//
//  KAAccountDetailViewController.m
//  KeepAccount
//
//  Created by Veer on 2017/12/25.
//  Copyright © 2017年 Veer. All rights reserved.
//

#import "KAAccountDetailViewController.h"
#import "KAAddAccountViewController.h"

#import "KAAccountModel.h"
#import "KACategoryModel.h"
#import "NSString+Tools.h"
#import "UIBarButtonItem+Item.h"

@interface KAAccountDetailViewController () <UIAlertViewDelegate>

@property (nonatomic, weak) UIView *topView;
@property (nonatomic, weak) UIImageView *iconImgView;
@property (nonatomic, weak) UILabel *nameLabel;

@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, weak) UIView *containerView;

@property (nonatomic, strong) NSMutableArray *detailLblArray;

@end

@implementation KAAccountDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.detailLblArray = [NSMutableArray array];
//    [self wr_setNavBarBackgroundAlpha:0];
    self.fd_prefersNavigationBarHidden = YES;
    
    [self setupSubView];
    [self setupNav];
    [self setupBottomView];
    [self loadData];
}


- (void)setupNav
{
//    UIBarButtonItem *backItem = [UIBarButtonItem backItemWithImage:[UIImage imageNamed:@"toolbar_navigation_icon_normal"] highImage:[UIImage imageNamed:@"toolbar_navigation_icon_pressed"] target:self action:@selector(back) norColor:RGB(51, 51, 51) highColor:RGB(120, 120, 120) title:@"返回"];
//
//    self.navigationItem.leftBarButtonItem = backItem;
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    // 设置标题
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    backButton.titleLabel.font = [UIFont systemFontOfSize:16];
    // 设置图片
    [backButton setImage:[UIImage imageNamed:@"toolbar_navigation_icon_normal"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"toolbar_navigation_icon_pressed"] forState:UIControlStateHighlighted];
    
    // 设置标题颜色
    [backButton setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
    [backButton setTitleColor:RGB(120, 120, 120) forState:UIControlStateHighlighted];
    backButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    // 自适应尺寸:自动根据按钮图片和文字计算按钮大小
    [backButton sizeToFit];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    // 设置按钮内容内边距
    backButton.x = 8;
    backButton.y = STATUS_BAR_HEIGHT;
    backButton.height = 44;
    [self.view addSubview:backButton];
}


- (void)setupSubView
{
    
    UIView *topView = [[UIView alloc] init];
    topView.backgroundColor = NAVColor;
    [self.view addSubview:topView];
    self.topView = topView;
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(NAVIGATION_BAR_HEIGHT + 68);
    }];
    
    // 头像
    UIImageView *iconImgView = [[UIImageView alloc] init];
    iconImgView.userInteractionEnabled = YES;
    iconImgView.image = [UIImage imageNamed:@"def_avatar"];
    iconImgView.contentMode = UIViewContentModeScaleAspectFill;
    iconImgView.layer.cornerRadius = 30;
    iconImgView.layer.masksToBounds = YES;
    [topView addSubview:iconImgView];
    self.iconImgView = iconImgView;
    [iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(STATUS_BAR_HEIGHT + 12);
        make.centerX.mas_equalTo(topView);
        make.width.height.mas_equalTo(60);
    }];

    // 手机号
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.text = @"购物";
    nameLabel.font = VRFont(16);
    nameLabel.textColor = RGB(51, 51, 51);
    [topView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(topView);
        make.top.mas_equalTo(iconImgView.mas_bottom).offset(12);
    }];
    
    //
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.alwaysBounceVertical = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.mas_equalTo(topView.mas_bottom);
        make.bottom.mas_equalTo(-50);
    }];
    
    UIView *containerView = [[UIView alloc] init];
    [scrollView addSubview:containerView];
    self.containerView = containerView;
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(scrollView);
        make.width.mas_equalTo(scrollView);
    }];
    
    NSArray *titleArray = @[@"典型", @"金额", @"日期", @"备注"];
    UIView *lastView = nil;
    for (int i = 0; i < titleArray.count; i++) {
        
        UIView *cellView = [self createCellWithTitle:titleArray[i] detail:nil];
        [containerView addSubview:cellView];
        [cellView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(i * 60);
            make.left.right.mas_equalTo(containerView);
            make.height.mas_equalTo(60);
        }];
        lastView = cellView;
    }
    
    [containerView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(lastView);
    }];
}


- (void)setupBottomView
{
    
    UIView *bottomBarView = [[UIView alloc] init];
    bottomBarView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomBarView];
    [bottomBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
        make.height.mas_equalTo(50 + HOME_INDICATOR_HEIGHT);
    }];
    
    UIView *hLineView = [[UIView alloc] init];
    hLineView.backgroundColor = RGB(225, 225, 225);
    [bottomBarView addSubview:hLineView];
    [hLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(bottomBarView);
        make.top.mas_equalTo(bottomBarView);
        make.height.mas_equalTo(0.5);
    }];
    
    UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    editBtn.tag = 100;
    [editBtn setTitle:@"编辑" forState:UIControlStateNormal];
    editBtn.titleLabel.font = VRFont(16);
    [editBtn setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
    [editBtn addTarget:self action:@selector(bottomBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bottomBarView addSubview:editBtn];
    [editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(bottomBarView.mas_centerX);
        make.height.mas_equalTo(50);
    }];

    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.tag = 101;
    [deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    deleteBtn.titleLabel.font = VRFont(16);
    [deleteBtn setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
    [deleteBtn addTarget:self action:@selector(bottomBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bottomBarView addSubview:deleteBtn];
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(bottomBarView.mas_centerX);
        make.right.mas_equalTo(bottomBarView);
        make.height.mas_equalTo(50);
    }];
    
    UIView *vLineView = [[UIView alloc] init];
    vLineView.backgroundColor = RGB(225, 225, 225);
    [bottomBarView addSubview:vLineView];
    [vLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(bottomBarView);
        make.centerY.mas_equalTo(editBtn);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(0.5);
    }];
}


- (UIView *)createCellWithTitle:(NSString *)title detail:(NSString *)detail
{
    UIView *cellView = [[UIView alloc] init];

    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.text = title;
    titleLbl.font = VRFont(16);
    titleLbl.textColor = RGB(154, 154, 154);
    [cellView addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(cellView);
        make.left.mas_equalTo(15);
    }];
    
    UILabel *detailLbl = [[UILabel alloc] init];
    detailLbl.text = title;
    detailLbl.font = VRFont(16);
    detailLbl.textColor = RGB(51, 51, 51);
    [cellView addSubview:detailLbl];
    [detailLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(cellView);
        make.left.mas_equalTo(titleLbl.mas_right).offset(15);
    }];
    
    // 线
    UIView *hLineView = [[UIView alloc] init];
    hLineView.backgroundColor = RGB(225, 225, 225);
    [cellView addSubview:hLineView];
    [hLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(cellView);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(cellView);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.detailLblArray addObject:detailLbl];
    
    return cellView;
}


#pragma mark - action
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)bottomBtnClick:(UIButton *)sender
{
    if (sender.tag == 100) {    // 编辑
        
        KAAddAccountViewController *addAccountVc = [[KAAddAccountViewController alloc] init];
        addAccountVc.curAccountModel = self.accountModel;
        WeakSelf(weakSelf)
        addAccountVc.addCountFinishedBlock = ^{
          
            [weakSelf loadData];
        };
        [self.navigationController pushViewController:addAccountVc animated:YES];
    }else{  // 删除
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"确认删除？" message:@"删除后数据不可恢复！" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"删除", nil];
        alert.delegate = self;
        [alert show];
    }
}


#pragma mark -
- (void)loadData
{
    
    self.iconImgView.image = [UIImage imageNamed:self.accountModel.categoryModel.image_s];
    self.nameLabel.text = self.accountModel.categoryModel.name;
    
    ((UILabel *)self.detailLblArray[0]).text = self.accountModel.categoryModel.type == 0 ? @"支出" : @"收入";
    ((UILabel *)self.detailLblArray[1]).text = [NSString moneyWith:self.accountModel.money];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSDate *date = [formatter dateFromString:self.accountModel.date];
    formatter.dateFormat = @"yyyy年MM月dd日 EEEE";
    ((UILabel *)self.detailLblArray[2]).text = [formatter stringFromDate:date];
    ((UILabel *)self.detailLblArray[3]).text = self.accountModel.note.length > 0 ? self.accountModel.note : self.accountModel.categoryModel.name;
}


#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {     // 删除
        
        [[KADataBase sharedDataBase] deleteAccount:self.accountModel];
        if ([self.delegate respondsToSelector:@selector(accountDetailViewControllerEditDidFinished)]) {
            [self.delegate accountDetailViewControllerEditDidFinished];
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
